package org.woosiyuan;

import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

import javax.imageio.ImageIO;

import ch.randelshofer.media.avi.AVIOutputStream;

public class Test2 {

	boolean mStopStream;
	
	String  flag = "Content-Length: ";
	String flag1 = "\r\n";
	
	public static void main(String[] args) throws Exception {
		Test2 test = new Test2();
		test.cameraStream();
	}

	public void save() throws IOException{
		URL aURL;
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		// try {
		aURL = new URL(
				"http://192.168.1.106:81/videostream.cgi?user=admin&password=admin");
		URLConnection conn = aURL.openConnection();
		conn.connect();
		InputStream input = conn.getInputStream();
		System.out.println("input = " + input);
		if (input != null)
			mStopStream = false;
		int readLength = -1;
		String strDate; // 将读取的数据转化为string类型，以便判断包头
		while (!mStopStream) {
			byte[] buffer = new byte[1024];
			readLength = input.read(buffer, 0, 1024);// readLength 本次读取数据的长度
			if (readLength > 0) {
				strDate = new String(buffer, readLength);
				// index标记"Content-Length: "的起始位置
				// index1标记"\r\n"的位置，注意是"Content-Length: "之后的第一个位置
				int index = strDate.indexOf(flag);
				int index1 = strDate.indexOf(flag1, index);
				int streamLength = 0;
				if (index != -1 && index1 != -1) {
					// 计算本次streamLength的长度
					streamLength = Integer.parseInt(strDate.substring(index
							+ flag.length(), index1));
				}
				if (streamLength > 0) {
					if ((index1 + 4) < readLength) {
						outStream.write(buffer, index1 + 4, readLength - index1
								- 4);
						streamLength = streamLength - readLength + index1 + 4;
					}
					// 将剩下读取的视频流存储到buffer1
					byte[] buffer1 = new byte[streamLength];
					int length = 0;
					while (length < streamLength) {
						length += input.read(buffer1, length, streamLength
								- length);

					}
					outStream.write(buffer1,0,streamLength);  // 将剩余的stream写入outStream
					byte[] data = outStream.toByteArray();}}}
	}
	
	
	
		public void cameraStream() throws IOException {
			
			URL aURL = new URL("http://192.168.1.106:81/videostream.cgi?user=admin&password=admin&rate=11");
			URLConnection conn = aURL.openConnection();
			conn.connect();
			InputStream input = conn.getInputStream();

			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			File file = new File("C:\\a.avi");
			AVIOutputStream aviOutputStream = new AVIOutputStream(file, AVIOutputStream.VideoFormat.JPG);
			
				System.out.println("input = " + input);
				if(input != null) mStopStream = false;
				int readLength = -1;
				String strDate;   //将读取的数据转化为string类型，以便判断包头
				int size = 0;
				while(!mStopStream && size<5*20) {
					byte[] buffer = new byte[1024*8];
					readLength = input.read(buffer,0,1024*8);//readLength 本次读取数据的长度
					if(readLength > 0) {
						strDate = new String(buffer, readLength);
						//index标记"Content-Length: "的起始位置
						//index1标记"\r\n"的位置，注意是"Content-Length: "之后的第一个位置
						int index = strDate.indexOf(flag);
						int index1 = strDate.indexOf(flag1,index);
						int streamLength = 0;
						if(index !=-1 &&index1 != -1) {
							//计算本次streamLength的长度
							streamLength = Integer.parseInt(strDate.substring(index+flag.length(), index1));
						}
						if(streamLength > 0) {
							if((index1+4) < readLength) {
								outStream.write(buffer, index1+4, readLength-index1-4);
								streamLength = streamLength - readLength+index1+4;
							}
							//将剩下读取的视频流存储到buffer1
							byte[] buffer1 = new byte[streamLength];
							int length = 0;
							while(length < streamLength) {
								length += input.read(buffer1,length,streamLength-length);
							}
							outStream.write(buffer1,0,streamLength);  // 将剩余的stream写入outStream
							byte[] data = outStream.toByteArray();
							BufferedImage bufferedImage = new BufferedImage(640, 320, BufferedImage.TYPE_3BYTE_BGR);
							ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
							bufferedImage = ImageIO.read(byteArrayInputStream);
//							Graphics2D graphics2d = bufferedImage.createGraphics();
//							graphics2d.drawImage(bufferedImage, 0, 0, 640, 320, null); 
//							graphics2d.setFont(new Font("宋体", Font.BOLD, 28)); 
//							graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
//							Date date = new Date();
//							graphics2d.drawString(date.toGMTString(), 50, 20);
//							graphics2d.dispose();	
							for(int i=0;i<6;i++){
							aviOutputStream.writeFrame(bufferedImage);
							}
							size++;
							outStream.reset();
							byteArrayInputStream.close();
						}
					}
				}
			outStream.close();
			aviOutputStream.close();
			
		}  // cameraStream()
	
}
